package com.tang.user.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tang.common.api.CompanyJobFeignClient;
import com.tang.common.entity.companyJob.Company;
import com.tang.common.entity.user.User;
import com.tang.user.mapper.UserMapper;

import com.tang.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 唐朝阳
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private JavaMailSender mailSender;

    @Override
    public User find(String userName, String password) {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("user_name", userName);
        param.put("password", password);
        param.put("delete_flag", 1);
        List<User> users = userMapper.selectByMap(param);
        if (!users.isEmpty()) {
            return users.get(0);
        }
        return null;
    }

    @Override
    public PageInfo<User> list(Integer userType, String keywords, Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        if (keywords == null || "".equals(keywords)) {
            QueryWrapper<User> qw = new QueryWrapper<User>();
            qw.eq("user_type", userType);
            qw.eq("delete_flag", 1);
            return PageInfo.of(userMapper.selectList(qw));
        }
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
        queryWrapper.eq("delete_flag", 1).eq("user_type", userType).
                and(qw -> qw.like("user_name", keywords).or().like("real_name", keywords));

        List<User> users = userMapper.selectList(queryWrapper);
        return PageInfo.of(users);
    }

    @Override
    public PageInfo<User> listExpectUser(Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        QueryWrapper<User> qw = new QueryWrapper<User>();
        qw.eq("delete_flag", 1);
        qw.eq("expect", "在校找工作").or().eq("expect", "离职找工作");
        List<User> users = userMapper.selectList(qw);
        return PageInfo.of(users);
    }

    @Override
    public Boolean noPassUser(String companyName) {
        QueryWrapper<User> q = new QueryWrapper<>();
        q.eq("delete_flag",1);
        q.eq("companyName", companyName);
        List<User> users = userMapper.selectList(q);
        Boolean flag = true;
        for (User user : users) {
            user.setHasAuth(2);
            int i = userMapper.updateById(user);
            if(i==0){
                flag = false;
            }
        }
        return flag;
    }

    @Override
    public PageInfo<User> listAuthUser(String keywords, Integer currentPage, Integer pageSize, Integer hasAuth) {
        PageHelper.startPage(currentPage, pageSize);
        if (keywords == null || "".equals(keywords)) {
            QueryWrapper<User> qw = new QueryWrapper<User>();
            qw.eq("delete_flag", 1);
            qw.isNotNull("auth_url");
            qw.eq("has_auth", hasAuth);
            return PageInfo.of(userMapper.selectList(qw));
        }
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
        queryWrapper.eq("delete_flag", 1).isNotNull("auth_url").eq("has_auth", hasAuth).
                and(qw -> qw.like("user_name", keywords).or().like("real_name", keywords));

        List<User> users = userMapper.selectList(queryWrapper);
        return PageInfo.of(users);
    }

    @Override
    public int deleteByUserId(int userId) {
        User user = userMapper.selectById(userId);
        user.setDeleteFlag(0);
        return userMapper.updateById(user);
    }

    @Override
    public int enableDisableByUserId(int userId) {
        User user = userMapper.selectById(userId);
        int result = 0;
        if (user != null && user.getDeleteFlag() != 0) {
            user.setState(user.getState() == 1 ? 0 : 1);
            user.setUpdateTime(new Date());
            result = userMapper.updateById(user);
        }
        return result;
    }

    @Override
    public int deleteByUserName(String userName) {
        User user = selectByUserName(userName);
        if (user != null) {
            user.setDeleteFlag(0);
        }
        int result = userMapper.updateById(user);
        return result;
    }

    @Override
    public User selectByUserName(String userName) {
        Map<String, Object> map = new HashMap<>();
        map.put("user_name", userName);
        map.put("delete_flag", 1);
        List<User> list = userMapper.selectByMap(map);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public int updateUser(User user) {
        QueryWrapper<User> qw = new QueryWrapper<User>();
        qw.ne("user_name", user.getUserName()).and(q -> q.eq("email", user.getEmail()).or().eq("tel", user.getTel()));
        User user1 = userMapper.selectOne(qw);
        if (user1 != null && user1.getDeleteFlag() == 0) {
            userMapper.deleteById(user1.getUserId());
        }
        int result = userMapper.updateById(user);
        return result;
    }

    @Override
    public int addUser(User user) {
        QueryWrapper<User> qw = new QueryWrapper<User>();
        qw.eq("user_name", user.getUserName()).or().eq("tel", user.getTel());
        User user1 = userMapper.selectOne(qw);
        if (user1 != null && user1.getDeleteFlag() == 0) {
            userMapper.deleteById(user1.getUserId());
        }
        int result = userMapper.insert(user);
        return result;
    }

    @Override
    public User selectByTel(String tel) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("tel", tel);
        map.put("delete_flag", 1);
        List<User> list = userMapper.selectByMap(map);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }


    @Override
    public User selectByEmail(String email) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("email", email);
        map.put("delete_flag", 1);
        List<User> list = userMapper.selectByMap(map);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public String auditCompanyUser(Integer userId, String res) {
        User user = userMapper.selectById(userId);
        if ("pass".equals(res) && user != null) {
            user.setHasAuth(1);
            user.setUserType(1);
            int i = userMapper.updateById(user);
            if (i != 0) {
                SimpleMailMessage message = new SimpleMailMessage();
                message.setFrom("1802361359@qq.com");
                message.setTo("15890224756@163.com");
                message.setSubject("千城百行企业用户认证通知");
                message.setText("您已经通过认证，可以登录千城百行招聘系统");
                mailSender.send(message);
                return "pass";
            }
        }
        if ("noPass".equals(res) && user != null) {
            user.setHasAuth(2);
            int i = userMapper.updateById(user);
            if (i != 0) {
                SimpleMailMessage message = new SimpleMailMessage();
                message.setFrom("1802361359@qq.com");
                message.setTo("15890224756@163.com");
                message.setSubject("千城百行企业用户认证通知");
                message.setText("您未通过认证，请重新进行认证");
                mailSender.send(message);
                return "noPass";
            }
        }
        return null;
    }

    @Override
    public Integer getUserCount(Integer filter) {
        QueryWrapper q = new QueryWrapper();
        if (filter == 0) {
            q.eq("user_type", 0);
        } else {
            q.eq("user_type", 1);
        }
        return userMapper.selectCount(q);
    }

    @Override
    public User selectById(Integer userId) {
        User user = userMapper.selectById(userId);
        return user;
    }

    @Override
    public List<User> selectByCompanyName(String companyName) {
        QueryWrapper<User> q = new QueryWrapper<>();
        q.eq("delete_flag", 1);
        q.eq("user_type", 1);
        q.eq("company_name", companyName);
        List<User> users = userMapper.selectList(q);
        return users;
    }
}
